<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增服务器</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
{% csrf_token %}

<form action="#"  onsubmit="return false">  {#return flase  禁用GET请求#}
    <h1>添加服务器信息</h1>
    主机名：<input type="text" name="hostname"><br>
    IP地址：<input type="text" name="ip"><br>
    操作系统：<input type="text" name="os"><br>
    硬件配置：<input type="text" name="hardware"><br>
    <input type="submit" value="提交" id="btn">
</form>

<script>
    $("#btn").click(function () {
        var csrf_token = $("[name='csrfmiddlewaretoken']").val();
        var hostname = $("[name='hostname']").val();
        var ip = $("[name='ip']").val();
        var os = $("[name='os']").val();
        var hardware = $("[name='hardware']").val();
        var data = {'hostname': hostname, 'ip': ip, 'os': os, 'hardware': hardware, 'csrfmiddlewaretoken': csrf_token}
        $.ajax({
            type: "POST",
            url: "/server/",
            data: data,
            success: function (result) {
                if (result.code == '0') {
                    console.log(result.msg);
                } else if (result.code == '1') {
                    alert(result.msg)
                }
            },
            error: function () {
                alert("请求服务器接口失败！")
            }
        })
    })
</script>
</body>
</html>